﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public static class Helper
{
    /// <summary>
    /// 说明：MessageBox用来在客户端弹出对话框。
    /// 参数：TxtMessage 对话框中显示的内容。
    /// 参数：Url 对话框关闭后，跳转的页
    /// </summary>
    public static string MessageBox(string TxtMessage, string Url)
    {
        string str;
        str = "<script language=javascript>alert('" + TxtMessage + "');location='" + Url + "'</script>";
        return str;
    }
    /// <summary>
    /// 说明：MessageBox用来在客户端弹出对话框,用法是Response（MessageBox（“你要弹出的话。”））；。
    /// 参数：TxtMessage 对话框中显示的内容。
    /// </summary>
    public static string MessageBox(string TxtMessage)
    {
        string str;
        str = "<script language=javascript>alert('" + TxtMessage + "')</script>";
        return str;
    }

    /// <summary>
    /// 获取数据库连接
    /// </summary>
    public static SqlConnection GetConnection()
    {
      string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
      SqlConnection con = new SqlConnection(conStr);
      return con;
    }

    /// <summary>
    /// 说明：ExecSQL用来执行SQL语句。
    ///	返回值：操作是否成功(True\False)。
    ///	参数：sqlStr SQL字符串
    /// </summary>
    public static Boolean ExecSQL(string sqlStr)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        try
        {
            myCmd.ExecuteNonQuery();
            myConn.Close();
        }
        catch
        {
            myConn.Close();
            return false;
        }
        return true;
    }
    /// <summary>
    /// 说明：ExecSQL用来执行SQL语句。
    ///	返回值：操作是否成功(True\False)。
    ///	参数：sqlStr SQL字符串
    /// </summary>
    public static Boolean ExecSQL(SqlConnection con, string sqlStr)
    {
        SqlCommand myCmd = new SqlCommand(sqlStr, con);
        try
        {
            myCmd.ExecuteNonQuery();
        }
        catch
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 说明：ExecuteScalar用来执行SQL语句。
    ///	返回值：返回查询的唯一结果。
    ///	参数：sqlStr SQL字符串
    /// </summary>
    public static string ExecuteScalar(string sqlStr)
    {
        string str;
        SqlConnection myConn = GetConnection();
        myConn.Open();
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
       
            str = myCmd.ExecuteScalar().ToString();
            myConn.Close();          
        return str;
    }

    /// <summary>
    /// 说明：GetDataSet数据集，返回数据源的数据集
    ///	返回值：数据集DataSet
    ///	参数：sqlStr SQL字符串，TableName数据表名称
    /// </summary>
    public static DataSet GetDataSet(string sqlStr, string TableName)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        var ds = GetDataSet(myConn, sqlStr, TableName);
        myConn.Close();
        return ds;
    }
    public static DataSet GetDataSet(SqlConnection con, string sqlStr, string TableName)
    {
        SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, con);
        DataSet ds = new DataSet();
        adapt.Fill(ds, TableName);
        return ds;
    }
    public static DataTable GetDataTable(SqlConnection con, string sqlStr)
    {
        return GetDataSet(con, sqlStr, "Result").Tables["Result"];
    }
    /// <summary>
    /// 执行一句SQL语句返回一个值。
    /// </summary>
    /// <param name="SQL">SQL语句</param>
    /// <returns>需要的值</returns>
    public static string GetOneInfo(string SQL)
    {
        SqlConnection myConn = GetConnection();
        SqlCommand myCmd = new SqlCommand(SQL, myConn);
        myConn.Open();
        string str = myCmd.ExecuteScalar().ToString();
        myCmd.Dispose();
        myConn.Close();
        return str;
    }
}
